﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.BaseAdapter;

public class AdapterFileBrowser extends BaseAdapter
{
    public final class ViewHolder
    {
        public ImageView fileIcon;
        public TextView fileName;
        public TextView fileInfo;
        public ImageView fileCheck;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    private String mMusicPath;

    public AdapterFileBrowser(Context context)
    {
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
    }

    public AdapterFileBrowser(Context context, List<Map<String, Object>> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    public void add(Map<String, Object> map)
    {
        mData.add(map);
    }

    public void removeItem(int position)
    {
        mData.remove(position);
    }

    public void updateItem(int position, String key, Object obj)
    {
        if (position < mData.size())
        {
            mData.get(position).put(key, obj);
        }
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        }
        else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        }
        else
        {
            return -1;
        }
    }
    
    public void setFilePath(String path)
    {
        mMusicPath = path;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent)
    {        
        ViewHolder holder = null;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.list_files_item, null);
            holder.fileIcon = (ImageView) convertView.findViewById(R.id.fileIcon);
            holder.fileName = (TextView) convertView.findViewById(R.id.fileName);
            holder.fileInfo = (TextView) convertView.findViewById(R.id.fileInfo);
            holder.fileCheck = (ImageView) convertView.findViewById(R.id.ivMusicCheck);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        Object fileIcon = (Object) mData.get(position).get("fileIcon");
        if (fileIcon instanceof Integer)
        {
            setViewImage((ImageView) holder.fileIcon, (Integer) fileIcon);
        } else if (fileIcon instanceof Drawable)
        {
            setViewImage((ImageView) holder.fileIcon, (Drawable) fileIcon);
        } else if (fileIcon instanceof String)
        {
            setViewImage((ImageView) holder.fileIcon, (String) fileIcon);
        }

        holder.fileName.setText((String) mData.get(position).get("fileName"));
        holder.fileInfo.setText((String) mData.get(position).get("fileInfo"));
        
        if(mMusicPath != null && mMusicPath.equals((String)mData.get(position).get("filePath")))
        {
            holder.fileCheck.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.fileCheck.setVisibility(View.GONE);
        }
        
                
        return convertView;
    }

    public void setViewImage(ImageView v, int value)
    {
        v.setImageResource(value);
    }

    public void setViewImage(ImageView v, String value)
    {
        // Bitmap bitmap = WebImageBuilder.returnBitMap(value);
        // ((ImageView) v).setImageBitmap(bitmap);
        // Bitmap很占内存，极其容易引起OutOfMemoryError,需要使用BitmapFactory.Options的对象对Bitmap进行修改

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(value, options);
        int srcHeight = options.outHeight;
        int srcWidth = options.outWidth;
        int scale = srcHeight / 48 + 1;

        BitmapFactory.Options newOptions = new BitmapFactory.Options();
        newOptions.inSampleSize = scale;
        newOptions.inJustDecodeBounds = false;
        newOptions.outHeight = srcHeight / scale;
        newOptions.outWidth = srcWidth / scale;
        bmp = BitmapFactory.decodeFile(value, newOptions);

        ((ImageView) v).setImageBitmap(bmp);
        if (bmp != null)
        {
            bmp.isRecycled();
            bmp = null;
        }
        System.gc();
    }

    public void setViewImage(ImageView v, Drawable value)
    {
        ((ImageView) v).setImageDrawable(value);
        value = null;
    }

}
